package com.px.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.dto.ReturnResult;
import com.px.dto.ReturnResultUtils;
import com.px.pojo.ShComment;
import com.px.service.ShCommentService;
import com.px.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/shComment")
public class ShCommentController {

    @Resource
    private ShCommentService shCommentService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private ObjectMapper objectMapper;

    @GetMapping("/list")
    public ReturnResult findUserByPage(@RequestParam(required = false) Integer commentType,
                                       @RequestParam(required = false) Integer commodityId,
                                       @RequestParam(required = false) Integer userId,
                                       @RequestParam(required = false) String pageIndex,
                                       @CookieValue(required = false) String account,
                                       Model model)throws Exception{
        Integer currentPageNo =1;
        //获取页码
        if(!StringUtils.isEmpty(pageIndex)){
            currentPageNo = Integer.parseInt(pageIndex);
        }
        //控制页码
        if(currentPageNo<=0){
            currentPageNo = 1;
        }
        Integer pageSize = 5;
        //查询出总记录数
        Integer totalCount = shCommentService.selectShCommentCount(commentType,commodityId,userId);
        //根据总记录数算出总页数
        Integer temp = totalCount/pageSize;
        Integer totalPageCount = totalCount%pageSize ==0? temp:temp+1;
        if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        List<ShComment> shComments = shCommentService.selectShCommentList(commentType, commodityId, userId, currentPageNo, pageSize);
        model.addAttribute("shComments",shComments);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("commentType",commentType);
        model.addAttribute("commodityId",commodityId);
        model.addAttribute("userId",userId);
        return ReturnResultUtils.returnSuccess(shComments);
    }

    @GetMapping("/view/{id}")
    public ReturnResult findUserList(@PathVariable Integer id, Model model)throws Exception{
        ShComment shComment = shCommentService.selectShComment(id);
        return ReturnResultUtils.returnSuccess(shComment);
    }

    @GetMapping("/toAdd/{id}")
    public String toAdd(@PathVariable Integer id, Model model)throws Exception{
        ShComment shComment = shCommentService.selectShComment(id);
        return "sysUser/add";
    }

    @GetMapping ("/add")
    public ReturnResult addShCommodity(ShComment shComment,Model model)throws Exception{
        int insert = shCommentService.insertShComment(shComment);
        ShComment shComment1 = shCommentService.selectShComment(shComment.getCommentId());
        if(insert > 0){
            return ReturnResultUtils.returnSuccess(shComment1);
        }else{
            return ReturnResultUtils.returnFail(1001,"失败");
        }
    }

    @GetMapping("/modify/{id}")
    public String toUpdate(@PathVariable Integer id, Model model)throws Exception{
        ShComment shComment = shCommentService.selectShComment(id);
        return "sysUser/update";
    }


    @GetMapping ("/update")
    public ReturnResult updateShCommodity(ShComment shComment,Model model)throws Exception{
        int update = shCommentService.updateShComment(shComment);
        ShComment shComment1 = shCommentService.selectShComment(shComment.getCommentId());
        if(update > 0){
            return ReturnResultUtils.returnSuccess(shComment1);
        }else{
            return ReturnResultUtils.returnFail(1001,"失败");
        }
    }

    @GetMapping("/del/{userid}")
    public ReturnResult deleteShCommodity(@PathVariable Integer userid,
                                          Model model)throws Exception{
        int delete = shCommentService.deleteShComment(userid);
        Map<String,Object> map = new HashMap<>();
        if(delete > 0){
            return ReturnResultUtils.returnSuccess();
        }else{
            return ReturnResultUtils.returnFail(1001,"失败");
        }
    }

}
